<template>
	<view class="balance">
		<view class="header">
			<text class="title">余额</text>
		</view>
		
		<view class="balance-info">
			<view class="balance-card">
				<text class="balance-label">当前余额</text>
				<text class="balance-amount">¥{{ balanceInfo.currentBalance }}</text>
				<view class="balance-tips">
					<text>可用余额：¥{{ balanceInfo.availableBalance }}</text>
					<text>冻结金额：¥{{ balanceInfo.frozenBalance }}</text>
				</view>
			</view>
		</view>
		
		<view class="recharge-section">
			<text class="section-title">充值金额</text>
			<view class="amount-grid">
				<view 
					v-for="amount in rechargeAmounts" 
					:key="amount"
					class="amount-item"
					:class="{ active: selectedAmount === amount }"
					@click="selectAmount(amount)"
				>
					<text class="amount">¥{{ amount }}</text>
					<text v-if="amount > 100" class="bonus">送¥{{ Math.floor(amount * 0.1) }}</text>
				</view>
				<view class="custom-amount">
					<input 
						v-model="customAmount" 
						type="number" 
						placeholder="输入其他金额"
						class="custom-input"
						@focus="selectCustomAmount"
					/>
				</view>
			</view>
			
			<button class="recharge-btn" @click="handleRecharge">立即充值</button>
		</view>
		
		<view class="records-section">
			<view class="section-header">
				<text class="section-title">充值记录</text>
				<text class="view-all" @click="viewAllRecords">查看全部</text>
			</view>
			<view class="records-list">
				<view v-for="record in recentRecords" :key="record.id" class="record-item">
					<view class="record-info">
						<text class="record-type">{{ record.type }}</text>
						<text class="record-time">{{ record.time }}</text>
					</view>
					<text class="record-amount" :class="{ positive: record.amount > 0 }">
						{{ record.amount > 0 ? '+' : '' }}¥{{ Math.abs(record.amount) }}
					</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
import { ref } from 'vue'

const balanceInfo = ref({
	currentBalance: 1288.50,
	availableBalance: 1188.50,
	frozenBalance: 100.00
})

const rechargeAmounts = ref([50, 100, 200, 500, 1000, 2000])
const selectedAmount = ref(100)
const customAmount = ref('')

const recentRecords = ref([
	{ id: 1, type: '充值', amount: 200, time: '2024-01-15 14:30' },
	{ id: 2, type: '消费', amount: -88, time: '2024-01-14 18:20' },
	{ id: 3, type: '充值', amount: 500, time: '2024-01-10 10:15' },
	{ id: 4, type: '退款', amount: 50, time: '2024-01-08 16:45' }
])

const selectAmount = (amount) => {
	selectedAmount.value = amount
	customAmount.value = ''
}

const selectCustomAmount = () => {
	selectedAmount.value = null
}

const handleRecharge = () => {
	const amount = customAmount.value || selectedAmount.value
	if (!amount || amount <= 0) {
		uni.showToast({
			title: '请输入有效金额',
			icon: 'none'
		})
		return
	}
	
	uni.showModal({
		title: '确认充值',
		content: `确定要充值 ¥${amount} 吗？`,
		success: (res) => {
			if (res.confirm) {
				uni.showToast({
					title: '充值成功',
					icon: 'success'
				})
				// 模拟充值成功
				balanceInfo.value.currentBalance += parseFloat(amount)
				balanceInfo.value.availableBalance += parseFloat(amount)
			}
		}
	})
}

const viewAllRecords = () => {
	uni.showToast({
		title: '查看全部记录',
		icon: 'none'
	})
}
</script>

<style scoped>
.balance {
	background: #f5f5f5;
	min-height: 100vh;
	padding: 20rpx;
}

.header {
	text-align: center;
	padding: 30rpx 0;
}

.title {
	font-size: 36rpx;
	font-weight: bold;
	color: #333;
}

.balance-info {
	margin-bottom: 40rpx;
}

.balance-card {
	background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
	border-radius: 20rpx;
	padding: 40rpx;
	color: white;
	text-align: center;
}

.balance-label {
	font-size: 24rpx;
	opacity: 0.8;
	display: block;
	margin-bottom: 15rpx;
}

.balance-amount {
	font-size: 56rpx;
	font-weight: bold;
	display: block;
	margin-bottom: 25rpx;
}

.balance-tips {
	display: flex;
	justify-content: space-around;
	font-size: 22rpx;
	opacity: 0.8;
}

.recharge-section {
	background: white;
	border-radius: 16rpx;
	padding: 30rpx;
	margin-bottom: 30rpx;
}

.section-title {
	font-size: 28rpx;
	font-weight: bold;
	color: #333;
	margin-bottom: 25rpx;
	display: block;
}

.amount-grid {
	display: grid;
	grid-template-columns: repeat(3, 1fr);
	gap: 20rpx;
	margin-bottom: 40rpx;
}

.amount-item {
	background: #f8f9fa;
	border: 2rpx solid #eee;
	border-radius: 12rpx;
	padding: 30rpx 20rpx;
	text-align: center;
	transition: all 0.3s;
}

.amount-item.active {
	border-color: #667eea;
	background: #f0f3ff;
}

.amount {
	font-size: 28rpx;
	font-weight: bold;
	color: #333;
	display: block;
	margin-bottom: 8rpx;
}

.bonus {
	font-size: 20rpx;
	color: #ff6b6b;
}

.custom-amount {
	grid-column: 1 / -1;
}

.custom-input {
	width: 100%;
	height: 80rpx;
	border: 2rpx solid #eee;
	border-radius: 12rpx;
	padding: 0 20rpx;
	font-size: 28rpx;
	text-align: center;
}

.custom-input:focus {
	border-color: #667eea;
}

.recharge-btn {
	background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
	color: white;
	border: none;
	border-radius: 50rpx;
	padding: 25rpx;
	font-size: 28rpx;
	font-weight: bold;
	width: 100%;
}

.records-section {
	background: white;
	border-radius: 16rpx;
	padding: 30rpx;
}

.section-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 25rpx;
}

.view-all {
	font-size: 24rpx;
	color: #667eea;
}

.records-list {
	display: flex;
	flex-direction: column;
	gap: 25rpx;
}

.record-item {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding-bottom: 25rpx;
	border-bottom: 1rpx solid #f0f0f0;
}

.record-item:last-child {
	border-bottom: none;
	padding-bottom: 0;
}

.record-info {
	display: flex;
	flex-direction: column;
}

.record-type {
	font-size: 26rpx;
	color: #333;
	margin-bottom: 8rpx;
}

.record-time {
	font-size: 22rpx;
	color: #999;
}

.record-amount {
	font-size: 28rpx;
	font-weight: bold;
}

.record-amount.positive {
	color: #52c41a;
}

.record-amount:not(.positive) {
	color: #ff4d4f;
}
</style>